#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'add_admin_form.ui'
#
# Created: Thu Apr 30 02:31:27 2009
#      by: The PyQt User Interface Compiler (pyuic) 3.17.6
#
# WARNING! All changes made in this file will be lost!


from qt import *
import anydbm

class add_admin_form(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("add_admin_form")



        self.groupBox3 = QGroupBox(self,"groupBox3")
        self.groupBox3.setGeometry(QRect(10,10,481,100))

        self.textLabel1 = QLabel(self.groupBox3,"textLabel1")
        self.textLabel1.setGeometry(QRect(10,30,150,20))

        self.admin_account_text = QLineEdit(self.groupBox3,"admin_account_text")
        self.admin_account_text.setGeometry(QRect(190,30,280,22))

        self.admin_password_text = QLineEdit(self.groupBox3,"admin_password_text")
        self.admin_password_text.setEchoMode(QLineEdit.Password)
        self.admin_password_text.setGeometry(QRect(190,60,280,22))


        self.textLabel1_2 = QLabel(self.groupBox3,"textLabel1_2")
        self.textLabel1_2.setGeometry(QRect(10,60,170,20))

        self.added_admin_btn = QPushButton(self,"added_admin_btn")
        self.added_admin_btn.setGeometry(QRect(370,120,120,27))

        self.cancel_added_btn = QPushButton(self,"cancel_added_btn")
        self.cancel_added_btn.setGeometry(QRect(240,120,120,27))

        self.languageChange()

        self.resize(QSize(505,160).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.added_admin_btn,SIGNAL("clicked()"),self.added_admin_btn_clicked)
        self.connect(self.cancel_added_btn,SIGNAL("clicked()"),self.cancel_added_btn_clicked)


    def languageChange(self):
        self.setCaption(self.__tr("Add admistrator"))
        self.groupBox3.setTitle(self.__tr("Add adminstrator"))
        self.textLabel1.setText(self.__tr("Adminstrator Account"))
        self.textLabel1_2.setText(self.__tr("Adminstrator password"))
        self.added_admin_btn.setText(self.__tr("&Add admin"))
        self.added_admin_btn.setAccel(QKeySequence(self.__tr("Alt+A")))
        self.cancel_added_btn.setText(self.__tr("&Cancel"))
        self.cancel_added_btn.setAccel(QKeySequence(self.__tr("Alt+C")))

    def added_admin_btn_clicked(self):
        """
        added_admin_btn_clicked(self):
            when clicking the "add admin" button, the program first check whether the name exists in the Database or not
            if the name is found, an error message will appear, if not the admin will be created
        """
        admin_db = anydbm.open('Admin_Accounts','c')

        admin_account = str(self.admin_account_text.text())
        admin_password = str(self.admin_password_text.text())
        if admin_db.has_key(admin_account):
            Message = 'This account is reserved , try other'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
        elif admin_account == '' or admin_password == '':
            Message = 'You must enter both account & password correctly!'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
        elif admin_account == '' and admin_password == '':
            Message = 'You must enter both account & password correctly!'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
        else:
            admin_db[admin_account] = admin_password
            Message = '%s account is successfully added'%admin_account
            QMessageBox.information(self,"Successfully operation",Message,QMessageBox.Ok)
            self.hide()

    def cancel_added_btn_clicked(self):
        self.hide()

    def __tr(self,s,c = None):
        return qApp.translate("add_admin_form",s,c)


